﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Newtonsoft.Json;

namespace NetJsWire.Direct.responsewrapper
{
    public class StoreLoadResponseWrapper
    {
        [JsonProperty]
        public Object additionalData;

        [JsonProperty]
        public bool comitted = true;

        [JsonProperty]
        public int results;

        [JsonProperty]
        public List<Object> rows;

        public StoreLoadResponseWrapper(DataRowCollection dataRows)
        {
            rows = new List<Object>();
            var arr = new DataRow[dataRows.Count];
            dataRows.CopyTo(arr, 0);
            rows.AddRange(arr);
            if (arr.Length > 0)
            {
                comitted = !arr[0].Table.DataSet.HasChanges();
            }
            results = rows.Count;
        }

        public StoreLoadResponseWrapper(DataRowCollection dataRows, int count)
        {
            rows = new List<Object>();
            var arr = new DataRow[dataRows.Count];
            dataRows.CopyTo(arr,0);
            rows.AddRange(arr);
            if (arr.Length > 0)
            {
                comitted = !arr[0].Table.DataSet.HasChanges();
            }
            results = count;
        }

        public StoreLoadResponseWrapper(DataRowCollection dataRows, int start, int limit)
        {
            rows = new List<Object>();
            var arr = new DataRow[dataRows.Count];
            dataRows.CopyTo(arr, 0);
            rows.AddRange(arr);
            if (arr.Length > 0)
            {
                comitted = !arr[0].Table.DataSet.HasChanges();
            }
            results = rows.Count;
            rows = page(rows, start, limit);
        }

        public StoreLoadResponseWrapper(DataRowCollection dataRows, int start, int limit, int count)
        {
            
            rows = new List<Object>();
            var arr = new DataRow[dataRows.Count];
            dataRows.CopyTo(arr, 0);

            
            rows.AddRange(arr);
            if (arr.Length > 0)
            {
                comitted = !arr[0].Table.DataSet.HasChanges();
            }
            rows = page(rows, start, limit);
            results = count;
        }


        public StoreLoadResponseWrapper(DataView dataRows)
        {
            rows = new List<Object>();
            var arr = new DataRowView[dataRows.Count];
            dataRows.CopyTo(arr, 0);
            rows.AddRange(arr);
            if (dataRows.Count > 0)
            {

                comitted = !dataRows.Table.DataSet.HasChanges(); ;
            }
            results = rows.Count;
        }

        public StoreLoadResponseWrapper(DataView dataRows, int count)
        {
            rows = new List<Object>();
            var arr = new DataRowView[ dataRows.Count];
            dataRows.CopyTo(arr, 0);
            rows.AddRange(arr);
            if (dataRows.Count > 0)
            {

                comitted = !dataRows.Table.DataSet.HasChanges(); ;
            }
            results = count;
        }

        public StoreLoadResponseWrapper(DataView dataRows, int start, int limit)
        {
            rows = new List<Object>();
            var arr = new DataRowView[ dataRows.Count];
            dataRows.CopyTo(arr, 0);
            rows.AddRange(arr);
            if (dataRows.Count > 0)
            {

                comitted = !dataRows.Table.DataSet.HasChanges(); ;
            }
            results = rows.Count;
            rows = page(rows, start, limit);
        }

        public StoreLoadResponseWrapper(DataView dataRows, int start, int limit, int count)
        {
            rows = new List<Object>();
            var arr = new DataRowView[dataRows.Count];
            dataRows.CopyTo(arr, 0);
            rows.AddRange(arr);
            if (dataRows.Count > 0)
            {

                comitted = !dataRows.Table.DataSet.HasChanges(); ;
            }
            rows = page(rows, start, limit);
            results = count;
        }


        public StoreLoadResponseWrapper(DataRow[] dataRows)
        {
            if (dataRows.Length > 0)
            {
                comitted = !dataRows[0].Table.DataSet.HasChanges();
            }
            rows = dataRows.ToList<Object>();
            results = dataRows.Length;
        }

        public StoreLoadResponseWrapper(DataRow[] dataRows, int count)
        {
            if (dataRows.Length > 0)
            {
                comitted = !dataRows[0].Table.DataSet.HasChanges();
            }
            rows = dataRows.ToList<Object>();
            results = count;
        }

        public StoreLoadResponseWrapper(DataRow[] dataRows, int start, int limit)
        {
            if (dataRows.Length > 0)
            {
                comitted = !dataRows[0].Table.DataSet.HasChanges();
            }
            rows = dataRows.ToList<Object>();
            rows = page(rows, start, limit);
            results = dataRows.Length;
        }

        public StoreLoadResponseWrapper(DataRow[] dataRows, int start, int limit, int count)
        {
            if (dataRows.Length > 0)
            {
                comitted = !dataRows[0].Table.DataSet.HasChanges();
            }
            rows = dataRows.ToList<Object>();
            rows = page(rows, start, limit);
            results = count;
        }

        private List<Object> page(List<Object> datarows, int start, int limit)
        {
            /*
            if (datarows.Count > 0)
            {
                // FIX ME vieleicht geht das ja so nicht da object nicht zwansläufig eine Table Propertie hat...
                this.comitted = !datarows[0].Table.DataSet.HasChanges();
            }*/
            var pagedRows = new List<Object>();
            for (var i = start; i < datarows.Count && i < (start + limit); i++)
            {
                pagedRows.Add(datarows[i]);
            }
            return pagedRows;
        }

    }
}
